<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Comments extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('Comments_model', '', TRUE);
    }
    
    public function create()
    {
        $this->load->library('form_validation');
        $this->load->helper('gravatar');
        $this->form_validation->set_rules('email', 'Email', 'valid_email|required');
        $this->form_validation->set_rules('content', 'Content', 'trim|required');
        $this->form_validation->set_rules('did', 'Did', 'is_natural_no_zero|required');
        $this->form_validation->set_rules('dtype', 'Dtype', 'is_natural_no_zero|required');
        $this->form_validation->set_rules('cap_wd', 'Captcha', 'valid_captcha|required');

        if ($this->form_validation->run() != FALSE) {
            if($this->Comments_model->create()){
                $message = '
                    <li>
                        <div class="author_avatar">
                            <img src="'.gravatar($this->input->post('email'),'G',50).' alt="avatar" />
                        </div>
                        <div class="prefix_1">
                            <time datetime="'.date('Y-m-d H:i:s',time()).'" pubdate="pubdate">'.date('Y-m-d H:i:s',time()).'</time>
                            <p>'.$this->input->post('content').'</p>
                        </div>
                    </li>
                ';
                $jdata = array(
                    'result' => 1,
                    'message' => $message
                );
                
                //update comment count
                
            }
        } else {
            $jdata = array(
                'result' => 0,
                'message' => validation_errors()
            );
        }
        
        echo json_encode($jdata);
    }

}